package com.superbytecode.cloud.common.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * All rights Reserved, Designed By www.super-master.cn
 *
 * @ProjectName: sanskrit
 * @Package: com.superbytecode.redis.context
 * @ClassName: SpringContextWrapper
 * @Description: [Spring环境上下文工具]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/8/14 18:20
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Component
public class SpringContextUtil implements ApplicationContextAware, EmbeddedValueResolverAware {
    // -----------------------------------------------------------------------------------------------------------------
    private static ApplicationContext applicationContext;
    private static StringValueResolver stringValueResolver;

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * 获取spring value
     * <p>
     * 和@Value的效果相同
     * note:如果是获取配置，找不到配置会报异常<
     * /p>
     *
     * @param spEl spring表达式，须用"${spEL}"括起来，否则返回值和入参相同
     * @return 表达式的值
     */
    public static String getSpringValue(String spEl) {
        return stringValueResolver.resolveStringValue(spEl);
    }

    // -----------------------------------------------------------------------------------------------------------------

    /**
     * 通过class获取bean
     * <p>
     * 获取实例，如果找不到会报错
     *
     * @param clazz class
     * @param <T>   返回的类型
     * @return Bean
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static <T> T getBean(String beanName, Class<T> clazz) {
        return applicationContext.getBean(beanName, clazz);
    }
    // -----------------------------------------------------------------------------------------------------------------

    /**
     * 获取当前profile
     */
    public static String getProfile() {
        String[] activeProfiles = applicationContext.getEnvironment().getActiveProfiles();
        return activeProfiles.length == 0 ? "dev" : activeProfiles[0];
    }

    /**
     * 获取当前配置属性
     */
    public static String getProperty(String configKey) {
        return applicationContext.getEnvironment().getProperty(configKey);
    }

    /**
     * 当前环境是否是生产环境
     */
    public static boolean isProd() {
        return "prod".equals(getProfile());
    }
    // -----------------------------------------------------------------------------------------------------------------

    /**
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * @param resolver
     */
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        SpringContextUtil.stringValueResolver = resolver;
    }
}